package com.wowangz.cms.dailyoperation.services.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.component.datagrid.pojo.DataGrid;
import com.wowangz.cms.component.datagrid.pojo.DataResult;
import com.wowangz.cms.component.datagrid.pojo.DataStyle;
import com.wowangz.cms.component.datagrid.pojo.PangingParam;
import com.wowangz.cms.component.datagrid.utils.DataTypeConstant;
import com.wowangz.cms.component.datagrid.utils.PangingTools;
import com.wowangz.cms.dailyoperation.services.pojo.Menu;
import com.wowangz.cms.dailyoperation.services.pojo.Services;
import com.wowangz.cms.dailyoperation.services.pojo.ServicesMenu;
import com.wowangz.cms.dailyoperation.services.service.MenuService;
import com.wowangz.cms.dailyoperation.services.service.ServicesMenuService;
import com.wowangz.cms.dailyoperation.services.service.ServicesService;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.api.BaseController;
import com.wowangz.cms.syscore.api.JSONFactory;
import com.wowangz.cms.syscore.utils.MessageResourcesUtils;
import com.wowangz.cms.syscore.utils.ServletUtils;

@Scope("prototype")
@Controller
public class ServicesCheckController extends BaseController{

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(ServicesCheckController.class);
	
	
	
	/**
	 * 点击菜单"服务复核"显示“服务复核”列表
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/dailyoperation/services/queryServicesCheckList.web")
	public String queryServicesCheckList(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		return "/WEB-INF/jsp/dailyoperation/services/servicescheckList";
	}
	
	/**
	 * 打开“服务复核”列表加载列表数据
	 * @param request
	 * @param response
	 * @param model
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/dailyoperation/services/queryServicesCheckListInfoDataGrid.json")
	public void queryServicesCheckListInfoDataGrid(HttpServletRequest request, HttpServletResponse response, Model model) {

		PangingParam pangingParam = null;
		DataResult dataResult = new DataResult();
		DataGrid dataGrid = null;

		try {
			pangingParam = (PangingParam) ServletUtils.getParametersToBaseEntity(request, PangingParam.class);
			HashMap<String, Object> map = (HashMap) ServletUtils.getParametersToHashMap(request);
			dataGrid = new DataGrid();
			dataGrid.setPangingParam(pangingParam);

			map.put("startrow", dataGrid.getStartrow());
			map.put("endrow", dataGrid.getEndrow());
			map.put("rows", dataGrid.getRows());
			map.put("sort", dataGrid.getSort());
			map.put("order", dataGrid.getOrder());
			map.put("modifyuserid", getUserContext().getLoginUser().getId());
			map.put("checkstatus", SysConstant.CheckStatus.SAVE);
			map.put("statusid", SysConstant.RecordStatus.VALID);

			ServicesService servicesService = (ServicesService) IssTransactionContainer.getInstance(ServicesService.class, false);
			long resultCount = servicesService.findResultCount(map);
			List<Services> resultList = servicesService.findResultList(map);

			DataStyle dataStyle = new DataStyle();
			dataStyle.setPageIndex(pangingParam.getPage());
			dataStyle.setDataCount(resultCount);
			dataStyle.setDataList(resultList);

			dataStyle.addDepict("id", DataTypeConstant.LONG);
			dataStyle.addDepict("servicenum", DataTypeConstant.STRING);
			dataStyle.addDepict("servicename", DataTypeConstant.STRING);
			dataStyle.addDepictByFunction("checkstatus", SysConstant.CheckStatus.class, "getName", new Class[]{long.class});
			dataResult = PangingTools.buildResultPagerInfo(dataStyle);

			if (pangingParam.getReportExportType() == 0) {
				response.getWriter().write(JSONFactory.toJSONString(dataResult, userContext));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 跳转服务复核页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/dailyoperation/services/toServicesCheckView.web")
	public String toServicesCheckView(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		ServicesMenuService servicesMenuService = (ServicesMenuService) IssTransactionContainer.getInstance(ServicesMenuService.class, false);
		ServicesService servicesService = (ServicesService) IssTransactionContainer.getInstance(ServicesService.class, false);
		HashMap<String,Object> hashMap = new HashMap<String, Object>();
		List<ServicesMenu> list = null;
		String id = getRequest().getParameter("id");
		hashMap.put("serviceid", id);
		try {
			Services services = servicesService.findservicesById(id);
			if(services.getCheckstatus() == SysConstant.CheckStatus.SAVE){
				//如果用户要修改的服务是保存状态，则给他修改的数据 读取dy_service_menu表中status=0的数据
				hashMap.put("status", SysConstant.ServiceMenuStatus.NEW);
				list = servicesMenuService.findservicesMenuByMap(hashMap);
			}else{
				//则读取status=1的数据
				hashMap.put("status", SysConstant.ServiceMenuStatus.OLD);
				list = servicesMenuService.findservicesMenuByMap(hashMap);
			}
			
			String str_ids = "";
			for(int i =0;i<list.size();i++){
				if("".equals(str_ids)){
					str_ids = String.valueOf(list.get(i).getMenuid());
				}else{
					str_ids = str_ids +","+String.valueOf(list.get(i).getMenuid());
				}
			}
			
			HashMap<String,Object> map=new HashMap<String,Object>();
			map.put("statusid",SysConstant.RecordStatus.VALID);
			map.put("menutype",SysConstant.MenuType.NORNAL);
			MenuService menuService = (MenuService) IssTransactionContainer.getInstance(MenuService.class, false);
			List<Menu> topNodesList = menuService.queryOtherMenuList(map);
			String menu_all = "";
			for(Menu menu:topNodesList){
				if("".equals(menu_all)){
					menu_all = ""+menu.getId();
				}else{
					menu_all = menu_all+","+menu.getId();
				}
			}
			
			model.addAttribute("menu_all", menu_all);
			model.addAttribute("services", services);
			model.addAttribute("str_ids", str_ids);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/WEB-INF/jsp/dailyoperation/services/servicescheckView";
	}
	
	/**
	 * 执行复核功能
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/dailyoperation/services/doServicesCheck.web",method={RequestMethod.GET,RequestMethod.POST})
	public String doServicesCheck(HttpServletRequest request, HttpServletResponse response, Model model){
		ServicesMenuService servicesMenuService = (ServicesMenuService) IssTransactionContainer.getInstance(ServicesMenuService.class, true);
		ServicesService servicesService = (ServicesService) IssTransactionContainer.getInstance(ServicesService.class, false);
		Services services  = (Services) ServletUtils.getParametersToBaseEntity(request, Services.class);
		try{
			services.setCheckstatus(SysConstant.CheckStatus.CHECKED);
			services.setBusinessstatus(SysConstant.CommonUsedStatus.START);
			services.setModifyuserid(getUserContext().getLoginUser().getId());
			services.setModifytime(new Date());
			servicesService.update(services);
			
			ServicesMenu servicesMenu = new ServicesMenu();
			servicesMenu.setServiceid(services.getId());
			//先删除老数据
			servicesMenu.setStatus(SysConstant.ServiceMenuStatus.OLD);
			servicesMenuService.deleteServicesMenu(servicesMenu);
			//再更新新数据
			servicesMenu.setStatus(SysConstant.ServiceMenuStatus.OLD);
			servicesMenuService.updateServicesMenu(servicesMenu);
			userContext.setInfoMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert8"));
			userContext.setCloseWindow(true);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return "/WEB-INF/jsp/dailyoperation/services/servicescheckList";
	}
}
